<?php //pack11class

class City24Object extends Module {

    public $category=NULL;
    public $oid=NULL;
    public $transaction=NULL;
    public $status="REO_STATUS_ACTIVE";
    public $price=NULL;
    public $info_lv=NULL;
    public $info_ru=NULL;

    public $pic1=NULL;
    public $pic2=NULL;
    public $pic3=NULL;
    public $pic4=NULL;
    public $pic5=NULL;
    public $pic6=NULL;
    public $pic7=NULL;
    public $pic8=NULL;
    public $pic9=NULL;
    public $pic10=NULL;
    public $pic11=NULL;

    public $county=NULL;
    public $parish=NULL;
    public $city=NULL;
    public $settlement=NULL;
    public $placename=NULL;
    public $street=NULL;
    public $street_num=NULL;
    public $square=NULL;
    public $plot_square=NULL;
    public $priority=NULL;
    public $show_in_xml=NULL;
    public $currency=NULL;
    public $floors=NULL;
    public $floor=NULL;
    public $rooms=NULL;



    public $object=NULL;
    public $properties=array();

    public $ready=false;

    public function setup(){
        
    }

    public function buildObject(DekaroObject $object) {
        
        $this->object=$object;
        $properties=$this->properties=$object->usedProperties();

        if(array_key_exists("category", $properties)) {
            $this->category=$properties["category"]->city24;
        } else { return; }

        $this->oid=$object->id;

        if(array_key_exists("transaction", $properties)) {
            $this->transaction=$properties["transaction"]->city24;
        } else { return; }

        if(array_key_exists("active", $properties)) {
            $this->status=($properties["active"])? "REO_STATUS_ACTIVE":"REO_STATUS_DELETED";
        }

        if(array_key_exists("object_currency", $properties)) {
            $currency=CurrencyManager::getCurrency($properties["object_currency"]->value);
            $this->currency=$currency->city24;
        } else { return; }

        if(array_key_exists("price", $properties) || array_key_exists("cost", $properties)) {
            if(array_key_exists("price", $properties)) {
                $this->price=$properties["price"]->value;
            } else {
                $this->price=$properties["cost"]->value;
            }
        } else { return; }
        
        $this->info_lv=$this->getText("lat");
        $this->info_ru=$this->getText("rus");

        $photos=$object->getPhotos();
        $ph=1;
        foreach($photos as $pic) {
            $ph_name="pic".$ph;
            $this->$ph_name="http://www.dekaro.lv/photos/large/".$pic->value;
            $ph++;
        }

        $this->county=$this->getCounty(); // COUNTY
        $cities=$this->getCity(); //CITY
        if(count($cities)==1) {
            $this->city=$cities[0]["city24"];
        } else if(count($cities)>1) {
            $this->parish=$cities[0]["value"]; // PARISH
            $this->city=$cities[count($cities)-1]["city24"];
        }
        if(array_key_exists("place", $properties)) {
            if($properties["place"]->city24!=$this->city) {
                $this->settlement=$properties["place"]->string; // SETTLEMENT
            }
        }
        if(array_key_exists("district", $properties)) {
            if($properties["district"]->city24!=$this->city) {
                $this->placename=$properties["district"]->string; // PLACENAME
            }
        }
        if(array_key_exists("street", $properties)) {
            $this->street=ValuesManager::getValueByID($properties["street"]->value); // STREET
        }

        if(array_key_exists("street_num", $properties)) {
            $this->street_num=$properties["street_num"]->string; // STREET_NUM
        }

        if(array_key_exists("square", $properties)) {
            $this->square=$properties["square"]->value; // SQUARE
        }

        if(array_key_exists("plot_square", $properties)) {
            $this->plot_square=$properties["plot_square"]->value; // PLOT_SQUARE
        }
        if($this->category=="land" && is_null($this->plot_square) && !is_null($this->square)) {
            $this->plot_square=$this->square;
            $this->square=null;
        }
        if(array_key_exists("special", $properties)) {
            $this->priority=1; // PRIORITY
            $this->show_in_xml="true"; // SHOW_IN_XML
        }
        if(array_key_exists("floors", $properties)) {
            $this->floors="
<floors>".$properties["floors"]->value."</floors>"; // FLOORS
        }
        if(array_key_exists("floor", $properties)) {
            $this->floor="
<floor>".$properties["floor"]->value."</floor>"; // FLOOR
        }
        if(array_key_exists("rooms", $properties)) {
            $this->rooms="
<rooms>".$properties["rooms"]->value."</rooms>"; // ROOMS
        }

        $this->ready=true;
    }

    private function getText($lang) {
        $lang_id=LanguagesManager::getLanguage($lang)->id;
        $result=MySQLGet::process("Select `text".$lang_id."` as `text` from ".MySQL::TABLE_OBJECTS." where `id`=".$this->object->id." limit 1;");
        return strip_tags(html_entity_decode($result[0]["text"]));
    }

    private function getCounty() {
        $group=PropertiesManager::getGroup(GroupsManager::getGroup("address")->id);
        foreach($group as $prp) {
            $prop_name=$prp->system;
            if(array_key_exists($prop_name, $this->properties)) {
                $property=$this->properties[$prop_name];
                if(strpos($property->city24,"COUNTY_")!==false) {
                    return $property->city24;
                }
            }
        }
        return null;
    }

    private function getCity() {
        $group=PropertiesManager::getGroup(GroupsManager::getGroup("address")->id);
        $result=array();
        foreach($group as $prp) {
            $prop_name=$prp->system;
            if(array_key_exists($prop_name, $this->properties)) {
                $property=$this->properties[$prop_name];
                if(strpos($property->city24,"CITY_")!==false) {
                    array_push($result,array("city24"=>$property->city24,"value"=>$property->string));
                    if($property->system=="city") return $result;
                }
            }
        }
        return $result;
    }
}
?>
